<?php
namespace plugins\alioss\lib;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Alioss
 *
 * @author Administrator
 */
class Alioss {
    private $config;

    private $endpoint;

    /**
     * @var \plugins\qiniu\QiniuPlugin
     */
    private $plugin;
    
    private $ossClient;
    
    private $storageRoot;
    private $bucket;

    /**
     * Qiniu constructor.
     * @param $config
     */
    public function __construct($config)
    {
        $pluginClass = cmf_get_plugin_class('Alioss');
        $this->plugin = new $pluginClass();
        $this->config = $this->plugin->getConfig();
        $this->endpoint = $this->config['endpoint'];
        $accessKey = $this->config['accessKey'];
        $secretKey = $this->config['secretKey'];
        $endpoint = $this->config['endpoint'];
        $this->ossClient = new \OSS\OssClient($accessKey,$secretKey,$endpoint);
        $this->storageRoot = '//'.$this->config['domain'].'/';
        $this->bucket = $this->config['bucket'];
    }

    /**
     * 文件上传
     * @param string $file 上传文件路径
     * @param string $filePath 文件路径相对于upload目录
     * @param string $fileType 文件类型,image,video,audio,file
     * @return mixed
     */
    public function upload($file, $filePath, $fileType = 'image')
    {
        $this->ossClient->uploadFile($this->bucket, $file,$filePath );
        $previewUrl = $fileType == 'image' ? $this->getPreviewUrl($file, $this->config['styles_icon']) : $this->getFileDownloadUrl($file);
        $url        = $fileType == 'image' ? $this->getImageUrl($file, $this->config['styles_watermark']) : $this->getFileDownloadUrl($file);

        return [
            'preview_url' => $previewUrl,
            'url'         => $url,
        ];
    }
    
    /**
     * 获取图片预览地址
     * @param string $file
     * @param string $wstyle
     * @return mixed
     */
    public function getPreviewUrl($file, $wstyle = '')
    {
        $style = empty($wstyle) ? 'styles_icon' : $wstyle;

        $url = $this->getUrl($file, $style);

        return $url;
    }

    /**
     * 获取图片地址
     * @param string $file
     * @param string $wstyle
     * @return mixed
     */
    public function getImageUrl($file, $wstyle = '')
    {
        $style  = empty($wstyle) ? 'watermark' : $wstyle;
        $config = $this->config;
        $url    = $this->storageRoot . $file;

        if (!empty($style)) {
            $url = $url . $config['style_separator'] . $style;
        }

        return $url;
    }

    /**
     * 获取文件地址
     * @param string $file
     * @param string $style
     * @return mixed
     */
    public function getUrl($file, $style = '')
    {
        $config = $this->config;
        $url    = $this->storageRoot . $file;
        if (!empty($style)) {
            $url = $url . $config['style_separator']. $style;
        }
        return $url;
    }

    /**
     * 获取文件下载地址
     * @param string $file
     * @param int $expires
     * @return mixed
     */
    public function getFileDownloadUrl($file, $expires = 3600)
    {
        $accessKey = $this->config['accessKey'];
        $secretKey = $this->config['secretKey'];
        $auth      = new Auth($accessKey, $secretKey);
        $url       = $this->getUrl($file);
        return $auth->privateDownloadUrl($url, $expires);
    }
    /**
     * 获取云存储域名
     * @return mixed
     */
    public function getDomain()
    {
        return $this->config['domain'];
    }
    
/**
     * 获取文件相对上传目录路径
     * @param string $url
     * @return mixed
     */
    public function getFilePath($url)
    {
        $parsedUrl = parse_url($url);

        if (!empty($parsedUrl['path'])) {
            $url            = ltrim($parsedUrl['path'], '/\\');
            $config         = $this->config;
            $styleSeparator = $config['style_separator'];
            $styleSeparatorPosition = strpos($url, $styleSeparator);
            if ($styleSeparatorPosition !== false) {
                $url = substr($url, 0, strpos($url, $styleSeparator));
            }
        } else {
            $url = '';
        }

        return $url;
    }
    
}
